package com.zhuiyi.yiqingbackend.controller;

import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.Base;
import com.zhuiyi.yiqingbackend.domain.request.AddBaseRequest;
import com.zhuiyi.yiqingbackend.domain.request.ChangeBaseStatus;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.BaseMapper;
import com.zhuiyi.yiqingbackend.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/base")
@Api(value = "base", tags = "住户表(base)的增删改查")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class BaseController {
    @Resource
    private BaseService baseService;
    @Resource
    private BaseMapper baseMapper;

    /**
     * 获取所有 base api
     * @return
     */
    @GetMapping("/getall")
    @ApiOperation(value = "all", notes = "获取所有base")
    public BaseResponse<List<Base>>getall() {
        List<Base> bases = baseService.getBases();
        return  ResultUtils.success(bases);
    }

    /**
     * 添加 base api
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "add", notes = "添加base")
    public BaseResponse add(@RequestBody AddBaseRequest request) {
        if (StringUtils.isBlank(request.getUname())) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "name不能为空");
        }
        if (StringUtils.isBlank(request.getPhone())) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "电话号码不能为空");
        }
        if (StringUtils.isBlank(request.getIdCard())) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "身份证不能为空");
        }
        Base base = new Base(request);
        boolean result = baseService.addBase(base);
        if (!result) {
            throw new BusinessException(ErrorCode.FAIL, "添加失败");
        }
        return ResultUtils.success("添加成功");
    }

    /**
     * 删除 base api
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "delete", notes = "删除base")
    public BaseResponse<Boolean> delete(@PathVariable Integer id) {
        boolean result = baseService.deleteBaseById(id);
        if (result) {
            return ResultUtils.success(result);
        } else {
            throw new BusinessException(ErrorCode.NO_DATA);
        }
    }

    /**
     * 更新 base api
     * @return
     */
    @PostMapping("/update")
    public BaseResponse update(@RequestBody Base base) {
        Integer id = base.getId();
//        System.out.println(base.getGender());
        if(id == null || id.equals("")) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "id不能为空");
        }
        boolean result = baseService.updateBase(base);
        if (result) {
            return ResultUtils.success("更新成功");
        } else {
            throw new BusinessException(ErrorCode.NO_DATA);
        }
    }
    /**
     * 更新 base 状态 api
     */
    @PostMapping("/updateStatus")
    public BaseResponse updateStatus(@RequestBody ChangeBaseStatus baseStatus) {
        int status = baseStatus.getUstatus();
        int id = baseStatus.getId();
//        System.out.println( id + " " + status);
        return ResultUtils.success(baseService.updateBaseStatus(baseStatus));
    }
    /**
     * 根据 id 获取 base api plus
     * @return
     */
    @GetMapping("/get2/{id}")
    @ApiOperation(value = "get", notes = "根据id获取base")
    public BaseResponse<List<Base>> get2(@PathVariable Integer id) {
        List<Base> list =new ArrayList<>();
        Base base = baseMapper.getBaseById(id);
        list.add(base);
        return ResultUtils.success(list);
    }
    /**
     * 根据 id 获取 base api
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "get", notes = "根据id获取base")
    public BaseResponse<Base> get(@PathVariable Integer id) {
        Base baseById = baseService.getBaseById(id);
        return ResultUtils.success(baseById);
    }

    /**
     * 根据手机号获取 base表信息  api
     * @return
     */
    @GetMapping("/getbyphone/{phone}")
    @ApiOperation(value = "getbyphone", notes = "根据手机号获取base")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query")
    public BaseResponse<Base> getByPhoneAndName(@PathVariable String phone) {
        boolean anyBlank = StringUtils.isAnyBlank(phone);// 检查手机号是否为空  true 为空
        if (anyBlank) {
            throw new BusinessException(ErrorCode.PARAMS_ERROE, "手机号不能为空");
        }
        Base base = baseService.getBaseByPhone(phone);
        return ResultUtils.success(base);
    }
}
